import { obj_str, uDb, obj_int } from 'sui';
import { g_db } from 'db/state';

const load_setting = (setMain, fnDone=null, sn="opt") => {
    uDb(fnDone, "file/get",
        {
            p: "setting",
            sn: sn,
        },
        r => {
            setMain(r);

            g_db.s_tech(obj_str(r, "tech"));
            g_db.s_dnum(obj_str(r, "num"));

            g_db.s_out_w_std(obj_str(r, "out_w_std"));
            g_db.s_out_dia_std(obj_str(r, "out_dia_std"));

            g_db.s_out_w_tech(obj_str(r, "out_w_tech"));
            g_db.s_out_dia_tech(obj_str(r, "out_dia_tech"));

            g_db.s_out_w_up(obj_str(r, "out_w_up"));
            g_db.s_out_w_down(obj_str(r, "out_w_down"));

            g_db.s_out_dia_up(obj_str(r, "out_dia_up"));
            g_db.s_out_dia_down(obj_str(r, "out_dia_down"));

            g_db.s_out_w_zero(obj_str(r, "out_w_zero"));
            g_db.s_out_dia_zero(obj_str(r, "out_dia_zero"));
        },
        e => setMain(null));
};

const set_setting = (val, setMain, setWait, herr, sn="opt") => {
    setWait(true);

    uDb(null, "file/md",
        {
            p: "setting",
            sn: sn,
            v: val,
        },
        r => load_setting(setMain, () => setWait(false)),
        e => {
            if (herr) {
                herr("设置出错");
            }

            setWait(false);
        }
    );
};

const get_val = (main, k) => {
    const v = obj_int(main, k);
    return v * 1000;
};

const utc_offset = () => {
    const cur = new Date();
    const off = cur.getTimezoneOffset();
    const ret = -off / 60;
    return ret;
};

export {utc_offset, get_val, set_setting, load_setting};
